/* 
 * The Client Example
 *
 * This example will connect to a remote server which responds to requests by giving
 * them the time. This is known as a daytime server. This example does assume that
 * you have an Internet connection.  If you do not have an Internet connection, you
 * will need to code a server and a client. In this case, read through the following
 * example and go on to the Client/Server example.
 */

package examples.socket;

import java.net.*;

import java.io.*;

/**
 *
 * @author prabhat.totoo
 */
public class TimeSocket {
    
   public static void main(String args[]) {
      Socket timeSocket = null;
      BufferedReader reader = null;
      
      try {
         /* The port number for the Daytime service is 13 */
         timeSocket = new Socket("tiglon.ecst.csuchico.edu", 13);
         
         /* In order to create the BufferedReader, we need
          * to wrap the InputStream in an InputStreamReader
          */
         reader = new BufferedReader(new InputStreamReader(timeSocket.getInputStream()));

         /* Read the string from the input stream  */
         String sTime = reader.readLine();

         /* Print the time to the console */
         System.out.println("According to the server, the time is: " + sTime);
         
         reader.close();
         timeSocket.close();
      }
      catch(UnknownHostException uhe) {
         System.out.println("Could not connect to host");
      }
      catch(IOException ioe) {
         System.out.println("I/O Exception occurred while using the socket.");
      }
   }
}